package com.crm.business.crm.controller;


import com.crm.business.crm.service.ICrmSourceService;
import com.crm.common.crm.entity.BO.CrmSourceSearchBO;
import com.crm.common.crm.entity.PO.CrmSource;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.entity.BasePage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/crmSource")
@Api(tags = "场景模块")
public class CrmSourceController {

    @Autowired
    private ICrmSourceService crmSourceService;

    @PostMapping("/querySourceDataGroupByCode")
    public Result<List<CrmSource>> querySourceDataGroupByCode(){
        return Result.ok(crmSourceService.querySourceDataGroupByCode());
    }

    @PostMapping("/queryList")
    public Result<BasePage<CrmSource>> queryList(@RequestBody CrmSourceSearchBO crmSource){
        return Result.ok(crmSourceService.queryList(crmSource));
    }

    @PostMapping("/queryInfoById/{id}")
    public Result<CrmSource> queryInfoById(@PathVariable("id") @ApiParam(name = "id", value = "id") Integer id){
        if(id == null || id == 0){
            return Result.error(500,"获取信息失败，主键为空");
        }
        return Result.ok(crmSourceService.queryInfoById(id));
    }

    @PostMapping("/addSource")
    public Result addSource(@RequestBody CrmSource crmSource){
        crmSourceService.addSource(crmSource);
        return Result.ok();
    }

    @PostMapping("/updateSource")
    public Result updateSource(@RequestBody CrmSource crmSource){
        crmSourceService.updateSource(crmSource);
        return Result.ok();
    }

    @PostMapping("/deleteByIds")
    public Result deleteByIds(@ApiParam(name = "ids", value = "id列表") @RequestBody List<Integer> ids) {
        try {
            crmSourceService.deleteByIds(ids);
        } catch (Exception exception) {
            exception.printStackTrace();
            return Result.error(500,"批量删除客户来源失败");
        }
        return Result.ok();
    }

    @PostMapping(value = "/deleteById/{id}")
    public Result deleteById(@PathVariable("id") @ApiParam(name = "id", value = "id") Integer id) {
        try {
            crmSourceService.deleteById(id);
        } catch (Exception exception) {
            exception.printStackTrace();
            return Result.error(500,"批量删除客户来源失败");
        }
        return R.ok();
    }


}
